package com.sbm.merge.controller;

import com.sbm.merge.entity.User;
import com.sbm.merge.service.UserService;
import com.sbm.merge.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname UserController
 * @Date 2020/11/18 15:56
 * @Created by 刘
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/hello")
    public String hello(){
//        userService
        return "hello";
    }

    //按照用户id查询
    @GetMapping("/user/{id}")
    public R getUserInfo(@PathVariable("id") Integer id){
        if (id!=null){
            //按照id查询
            User user = userService.getUserById(id);
            return new R(0,user);
        }else {
            //查询全部
            List<User> users = userService.getAllUser();
            return new R(0,users);
        }

    }

    //删除
    @GetMapping("/user/delete/{id}")
    public R deleteUser(@PathVariable Integer id){
        try {
            userService.deleteUserById(id);
            return new R(0,"删除成功");
        }catch (Exception e){
            return new R(-1,"删除失败");
        }

    }

    //修改
    @PostMapping("/user/update/{id}")
    public R updateUser(@RequestBody User user,Integer id){
        try {
            userService.updateUser(user,id);
            return new R(0,"更新成功");
        }catch (Exception e){
            return new R(-1,"更新失败");
        }
    }

    //增加用户
    @PostMapping("/user/add")
    public R addUser(@RequestBody User user){
        try {
            userService.addUser(user);
            return new R(0,"新增成功");
        }catch (Exception e){
            return new R(-1,"新增失败");
        }
    }





}
